/*
 * LoRa_BSP.c
 *
 *  Created on: 2021年3月18日
 *      Author: gary
 */

#include "LoRa_BSP.h"

LoRa_BSP_IO_Typedef LoRaBSP0;
LoRa_BSP_IO_Typedef LoRaBSP1;

void LoRa_BSP_Init(void)
{
  LoRaBSP0.GPIO_CS_Port = LoRa0_CS_GPIO_Port;
  LoRaBSP0.GPIO_CS_Pin = LoRa0_CS_Pin;
  LoRaBSP0.GPIO_RESET_Port = LoRa0_CS_GPIO_Port;
  LoRaBSP0.GPIO_RESET_Pin = LoRa0_CS_Pin;
  LoRaBSP0.GPIO_DIO_Port = LoRa0_CS_GPIO_Port;
  LoRaBSP0.GPIO_DIO_Pin = LoRa0_CS_Pin;
  LoRaBSP0.LoRa_BSP_SPI = &hspi0;
}

uint8_t LoRa_SingleTransfer(LoRa_BSP_IO_Typedef* LoRaBSP, uint8_t address, uint8_t value)
{
  uint8_t sendBuffer[2], readBuffer[2];
  sendBuffer[0] = address;
  sendBuffer[1] = value;
  HAL_GPIO_WritePin(LoRaBSP->GPIO_CS_Port, LoRaBSP->GPIO_CS_Pin, GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive(LoRaBSP->LoRa_BSP_SPI, sendBuffer, readBuffer, 2, HAL_MAX_DELAY);
  HAL_GPIO_WritePin(LoRaBSP->GPIO_CS_Port, LoRaBSP->GPIO_CS_Pin, GPIO_PIN_SET);
  return readBuffer[1];
}

uint8_t LoRa_ReadRegister(uint8_t address)
{
  return LoRa_singleTransfer(address & 0x7f, 0x00);
}

void LoRa_WriteRegister(uint8_t address, uint8_t value)
{
  LoRa_singleTransfer(address | 0x80, value);
}


